package Presentation.GraphicUI;

import BusinessLogic.ControllerFacade;
import BusinessLogic.Exceptions.AuthenticationFailedException;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.Map;

public abstract class MovieSystemPanel extends JPanel {
    protected MainScreen m_mainScreen;
    protected ControllerFacade m_controller;

    public MovieSystemPanel(MainScreen mainScreen){
        super();
        this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));

        m_mainScreen = mainScreen;
        m_controller = mainScreen.m_controllerFacade;
        this.setBorder(BorderFactory.createLoweredBevelBorder());
    }

    public void setUp(){}

    public static JPanel createButtonsPanel(Map<Integer,JButton> m_buttons){
        JPanel m_buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JPanel j = new JPanel(new GridLayout(m_buttons.size(), 1, 10, 10));

        for(JButton button: m_buttons.values()){
            j.add(button);
            m_buttonsPanel.add(j);            
        }

        return m_buttonsPanel;
    }

    public static JPanel createFormPanel(Map<Integer,JComponent> m_components, JButton submitButton){
        JPanel m_formPanel = new JPanel(new GridLayout(m_components.size() + 1, 1));

        for(JComponent comp: m_components.values()){
            JPanel j = new JPanel(new FlowLayout(FlowLayout.LEFT));
            JLabel firstNameLabel = new JLabel(comp.getName());
            j.add(firstNameLabel);
            j.add(comp);
            m_formPanel.add(j);
        }
        JPanel j = new JPanel(new FlowLayout(FlowLayout.LEFT));  
        j.add(submitButton);
        m_formPanel.add(j);
        return m_formPanel;
    }    

    public static JPanel createInstructionLabel(String msg){        
        return createInstructionLabel(msg, new JLabel[1]);
    }

    public static JPanel createInstructionLabel(String msg,JLabel[] j){
        JPanel m_msgPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        j[0] = new JLabel(msg);
        m_msgPanel.add(j[0]);
        return m_msgPanel;
    }

    public JTextField createTextField(String label, int size) {
        JTextField comp = new JTextField(size);
        comp.setName(label);
        return comp;
    }

    public JPasswordField createPasswordField(String label, int size) {
        JPasswordField comp = new JPasswordField(size);
        comp.setName(label);
        return comp;
    }

    public JComboBox createYesNoField(String label) {
        JComboBox comp = new JComboBox();
        comp.addItem("Yes");
        comp.addItem("No");
        comp.setName(label);
        return comp;
    }

    public JComboBox createComboBox(String label, Collection<String> items) {
        JComboBox comp = new JComboBox();
        for (String s : items)
            comp.addItem(s);
        comp.setName(label);
        return comp;
    }    

    public JButton createNavigationButton(String label,final String panel){
        JButton button = new JButton(label);
        button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    m_mainScreen.setActivePanel(panel);
                }
            });
        return button;
    }

    public JButton createLogoutButton(){
        JButton button = new JButton("  Logout  ");
        button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    try {
                        m_controller.logout();
                    } catch (AuthenticationFailedException e1) {
                        JOptionPane.showMessageDialog(m_mainScreen, e1.getMessage());
                    }

                    m_mainScreen.setActivePanel(MainScreen.MAIN_MENU);
                }
            });
        return button;
    }
}
